package com.coderman.java8.actions;

import javax.swing.text.html.Option;
import java.util.Optional;

/**
 * @Author fanchunshuai
 * @Date 2019/12/3 16
 * @Description:
 */
public class OptionlDemo {
    public static void main(String[] args) {
        DataPerson dataPerson = new DataPerson();
        Optional<Object> optional = Optional.of(dataPerson);
        //对象为空则打出 -
        System.out.println(optional.isPresent()?optional.get():"-");
        //对象的属性为空则打出 -
        Optional<String> name = Optional.ofNullable(dataPerson.getName());
        System.out.println(name.isPresent() ? name.get():"-");

        //如果不为空，则打印出xxx
        Optional.ofNullable("test").ifPresent(na-> System.out.println(na+"ifPresent"));

        //如果是空，则返回指定字符串
        System.out.println(Optional.ofNullable(null).orElse("-"));
        System.out.println(Optional.ofNullable("1").orElse("1"));

        //如果是空则返回指定方法或者代码
        Optional.ofNullable(null).orElseGet(()->{
            return "hhhhhh";
        });

        //如果是空，则可以抛出异常
        System.out.println(Optional.ofNullable("1").orElseGet(()->{return "ghhhhh";}));
        System.out.println(Optional.ofNullable("1").orElseThrow(()->{
            throw new RuntimeException("eee");
        }));







    }
}
